package com.ruoyi.apis.configuration;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.Contact;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Choi
 * @Date 2021/7/21
 */

@Configuration
@EnableSwagger2
public class SwaggerDocConfiguration {

    //引入扩展类
    private final OpenApiExtensionResolver openApiExtensionResolver;

    @Autowired
    public SwaggerDocConfiguration(OpenApiExtensionResolver openApiExtensionTesolver) {
        this.openApiExtensionResolver = openApiExtensionTesolver;
    }

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
         //根据每个方法名也知道当前方法在设置什么参数
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("高领展示屏接口文档")
                        .description("高领展示屏接口文档")
                        .license("Choi")
                        .contact(new Contact("Choi", "http://blog.kelu.pro", "czhd2552@163.com"))
                        .version("1.0")
                        .build())
                .groupName("1.0")
//                .globalRequestParameters(this.parameters())
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .extensions(openApiExtensionResolver.buildExtensions("1.0"));
        return docket;
    }

    /**
     * 配置header头请求参数token
     * @return
     */
    private List<RequestParameter> parameters() {
        RequestParameterBuilder parameterBuilder = new RequestParameterBuilder();
        List<RequestParameter> parameters = new ArrayList<>();
        parameterBuilder.name("token")
                .description("token令牌")
                .in("header")
                .required(false)
                .build();
        parameters.add(parameterBuilder.build());
        return parameters;
    }
}
